package com.bw.flinkstreaming.transform;
import com.bw.flinkstreaming.source.job2.NotParallelSource;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
/**
 * 通过map和filter函数完成如下案例
 */
public class MapAndFilterDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<Long> numberStream = env.addSource(new NotParallelSource()).setParallelism(1);
        /*
        * MapFunction<T, O>
        * @param <T> Type of the input elements.（输入元素的类型）
        * @param <O> Type of the returned elements.（输出元素的类型）
        *
        * */
        SingleOutputStreamOperator<Long> dataStream = numberStream.map(new MapFunction<Long, Long>() {
            @Override
            public Long map(Long value) throws Exception {
                System.out.println("接受到了数据："+value);
                return value+1;
            }
        });

        /**
         * FilterFunction<T>
         * @param <T> The type of the filtered elements.
         */
        SingleOutputStreamOperator<Long> filterDataStream = dataStream.filter(new FilterFunction<Long>() {
            @Override
            public boolean filter(Long number) throws Exception {
                return number % 2 == 0;//true
            }
        });

        filterDataStream.print().setParallelism(1);
        env.execute("MapAndFilterDemo");
    }
}